package org.jeecg.modules.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.desensitization.enums.SensitiveEnum;
import org.jeecg.common.desensitization.util.SensitiveInfoUtil;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppClientMenu;
import org.jeecg.modules.app.entity.AppMenu;
import org.jeecg.modules.app.entity.AppUpgrade;
import org.jeecg.modules.app.service.IAppClientMenuService;
import org.jeecg.modules.app.service.IAppClientService;
import org.jeecg.modules.app.service.IAppMenuService;
import org.jeecg.modules.app.service.IAppUpgradeService;
import org.jeecg.modules.app.vo.UpgradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @Description: 应用接口
* @Author: sean
* @Date:   2024-09-28
* @Version: V1.0
*/
@Api(tags="应用接口")
@RestController
@RequestMapping("/app/api")
@Slf4j
@RequiredArgsConstructor
public class ApiController{

    private final IAppClientService appClientService;

    private final IAppClientMenuService appClientMenuService;

    private final IAppMenuService appMenuService;

    @ApiOperation(value="通过appId查询", notes="通过appId查询")
    @GetMapping(value = "/queryByAppId")
    public AppClient queryByAppId(@RequestParam(name="appId",required=true) String appId) {
        AppClient appClient = appClientService.getOne(new QueryWrapper<AppClient>().eq("app_id", appId));
        if(oConvertUtils.isNotEmpty(appClient) && oConvertUtils.isNotEmpty(appClient.getAppSecret())){
            appClient.setAppSecret(SensitiveInfoUtil.getEncodeData(appClient.getAppSecret(), SensitiveEnum.ENCODE));
        }
        return appClient;
    }

    @ApiOperation(value="通过appId查询", notes="通过appId查询")
    @GetMapping(value = "/menusList")
    public List<String> menuList(@RequestParam(name="appId",required=true) String appId) {
        AppClient appClient = appClientService.getOne(new QueryWrapper<AppClient>().eq("app_id", appId));
        if(oConvertUtils.isNotEmpty(appClient)){
            List<AppClientMenu> list = appClientMenuService.list(new QueryWrapper<AppClientMenu>().lambda().eq(AppClientMenu::getClientId, appClient.getId()));
            Set<Long> menuIdList = list.stream().map(AppClientMenu::getMenuId).collect(Collectors.toSet());
            if(null != menuIdList && !menuIdList.isEmpty()){
                List<AppMenu> menuList = appMenuService.listByIds(menuIdList);
                return menuList.stream().map(AppMenu::getCode).collect(Collectors.toList());
            }
            return new ArrayList<>();
        }
        return new ArrayList<>();
    }

}
